const {http} = require("../../utils/http")
Page({
    data:{
        musics:[],
        /* 记录前一个播放的item的id */
        prevId:""
    },
    onSubmit(event){
        // console.log(event.detail.value)
        var keyword  = event.detail.value.name;
        var url  = `http://47.108.197.28:3000/search?keywords=${keyword}`;
        http({
            url,
            success:res=>{
                var musics = [];
                var songs = res.data.result.songs;
                songs.forEach(item=>{
                    var {name,id,artists,duration}=item;
                    musics.push({
                        name,
                        id,
                        author:artists[0].name,
                        duration,
                        isPlay:false
                    })
                })
                this.setData({
                    musics
                })
            }
        })
    },
    handleClick(event){
        /* 正在点击的id */
        var {aid} =event.currentTarget.dataset;
        var  prevId= this.data.prevId;
        var  musics = this.data.musics;
        /* 只要正在点击的id和之前的id不同,就可以让音乐进入播放的状态 */
        if(aid !=prevId){
            musics.forEach(item=>{
                if(item.id == aid){
                    item.isPlay = true;
                }else{
                    item.isPlay = false;
                }
            })
            this.setData({
                musics,
                prevId:aid
            })
        }else{
            musics.forEach(item=>{
                if(item.id == aid){
                    item.isPlay =  !item.isPlay;
                }
            })
            this.setData({
                musics
            })
        }
        
    }
})
